/**
 * 表单处理类
 * 版本：1.0.0
 */

class InputConfig {
  public maxLength: number; // 输入长度
  public type?: string; // 输入款类型
  public inputBlur?: any; // 输入框的change事件
  public changeFun?: any; //
}

class SelectConfig {
  public multiple?: boolean; // 是否多选
  public options?: Array<any>; // 下拉框数据
  public dictType?: string; // 字典类型
  public selectedHide?: boolean; // 选中隐藏
  public maxTagCount?: number; // 显示几个开始隐藏
  public allowClear?: boolean; // 是否可以清空
  public confirmFun?: any; // 确认回调，formInfo：提交数据对象,that：当前对象
  public disabled?: boolean; // 禁用
}

class SwitchConfig {
}

class TextareaConfig {
  public maxLength: number; // 输入长度
  public rows: number; // 多少行
  public disabled?: boolean; // 禁用
}

class InputNumberConfig {
  public min?: number; // 最小值
  public max?: number; // 最大值
  public step?: number; // 每次加及
  public precision?: number; // 	数值精度
}

class TextConfig {


}

class SelectGroup {
  public multiple?: boolean; // 是否多选
  public groupMap?: any; // 下拉框数据
  public dictType?: string; // 字典类型
  public selectedHide?: boolean; // 选中隐藏
  public maxTagCount?: number; // 显示几个开始隐藏
  public allowClear?: boolean; // 是否可以清空
}

class SelectTree {
  public trees: Array<any>; // 下拉框数据
  public checkable?: boolean; // 打开多选
  public allowClear?: boolean; // 是否可以清空
  public maxTagCount?: number; // 显示几个开始隐藏
}

class DateConfig {

}

class UploadPicture {
  public loading?: boolean; // 加载中
  public clickFun?: any; // 点击事件调用方法，formInfo：数据对象,component：当前配置

}

export class LinkageChildren {
  public name: string; // 名称
  public field: string; // 字段
  public initOptionFun?: any; // 初始化选项，$event:打开关闭，true:打开，false：关闭，formInfo：提交form对象,that：当前对象
  public confirmFun?: any; // 确认回调，formInfo：提交数据对象,that：当前对象
  public disabled?: boolean; // 是否禁用
  public placeholder?: string; // 描述
  public children?: Array<LinkageChildren>;
  public dictType?: string; // 字典类型
  public options?: Array<any>;
}

class LinkageSelect {
  public children?: Array<LinkageChildren>;
  public initFun?: any; // 初始化回调，由父级调用,formInfo：提交数据对象,that：当前对象
}

class HeaderConfig {
}

class ButtonConfig {
  public icon?: string; // 图标
  public confirmFun?: any; // formInfo：提交数据对象,component:当前配置
}

class UploadInput {
  public isUpload?: boolean; // 是否是上传
  public confirmFun?: any; // formInfo：提交数据对象,component:当前配置
}


export class ArrayConfig {
  public name: string;   // 名称
  public field: string;  // 字段
  public required?: boolean; // 是否必填
  public styleClass?: string; // 样式
  public input?: InputConfig;
  public select?: SelectConfig;
  public switch?: SwitchConfig;
  public textarea?: TextareaConfig;
  public inputNumber?: InputNumberConfig;
  public text?: TextConfig;
  public selectGroup?: SelectGroup;
  public selectTree?: SelectTree;
  public date?: DateConfig;
  public uploadPicture?: UploadPicture;
  public linkageSelect?: LinkageSelect;
  public header?: HeaderConfig;
  public button?: ButtonConfig;
  public uploadInput?: UploadInput;
}

class FormConfig {
  public components: Array<ArrayConfig>;
  public styleClass?: string; // 样式
}

export class FormInfoModel {
  public formConfig: FormConfig;
  public formInfo: any;

  constructor() {
    this.formInfo = {};
    this.formConfig = new FormConfig();
  }


  initForm(formConfig: FormConfig, formInfo) {
    this.formConfig = formConfig;
    this.formInfo = formInfo;
    if (!this.formInfo) {
      this.formInfo = {};
    }
    for (const component of formConfig.components) {
      if (!this.formInfo[component.field] && this.formInfo[component.field] !== 0) {
        this.formInfo[component.field] = null;
      }
    }
    for (const component of this.formConfig.components) {
      if (component.linkageSelect && component.linkageSelect.initFun) {
        component.linkageSelect.initFun(this.formInfo, component);
      }
    }
  }

  getDisabled(){
    if(this.formConfig.components){
      for(const component of this.formConfig.components){
        if(component.required){
          if(!this.formInfo[component.field]||!this.formInfo[component.field].trim()){
            return true;
          }
        }
      }
    }
    return false;
  }
}
